#!/bin/bash

echo "=========================================="
echo "  Converting OS Image to VMware Format"
echo "=========================================="
echo

# 检查os-image.bin
if [ ! -f os-image.bin ]; then
    echo "[ERROR] os-image.bin not found!"
    echo "Please run build.sh first."
    exit 1
fi

# 检查qemu-img
if ! command -v qemu-img &> /dev/null; then
    echo "[ERROR] qemu-img not found!"
    echo
    echo "Install it with:"
    echo "  Ubuntu/Debian: sudo apt install qemu-utils"
    echo "  Fedora/RHEL:   sudo dnf install qemu-img"
    echo "  Arch Linux:    sudo pacman -S qemu"
    echo "  macOS:         brew install qemu"
    exit 1
fi

echo "[INFO] Using qemu-img: $(which qemu-img)"
echo "[INFO] Converting to VMDK format..."
echo

qemu-img convert -f raw -O vmdk os-image.bin os-disk.vmdk

if [ $? -eq 0 ]; then
    echo "[OK] Conversion successful!"
    echo "[OK] Created: os-disk.vmdk"
    echo
    ls -lh os-disk.vmdk
    echo
    echo "=========================================="
    echo "  Next Steps to Run in VMware:"
    echo "=========================================="
    echo
    echo "1. Open VMware Workstation/Player/Fusion"
    echo "2. Create New Virtual Machine"
    echo "3. Select 'Custom' configuration"
    echo "4. Guest OS: 'Other' - 'Other'"
    echo "5. Firmware: 'BIOS' (NOT UEFI!)"
    echo "6. Memory: 128 MB or more"
    echo "7. Disk: Use existing disk - select os-disk.vmdk"
    echo "8. Start the VM and enjoy!"
    echo
    
    # 也创建VirtualBox版本
    echo "Also creating VirtualBox format..."
    qemu-img convert -f raw -O vdi os-image.bin os-disk.vdi
    if [ $? -eq 0 ]; then
        echo "[OK] Created: os-disk.vdi (for VirtualBox)"
        ls -lh os-disk.vdi
    fi
    
else
    echo "[ERROR] Conversion failed!"
    exit 1
fi

